package com.ctgu.tengyun.controller.role;

import com.ctgu.tengyun.domain.SysRole;
import com.ctgu.tengyun.manager.UserRoleScheduleManager;
import com.ctgu.tengyun.service.role.SysRoleService;
import com.ctgu.tengyun.util.StringUtils;
import com.ctgu.tengyun.util.Validator;
import com.google.common.collect.ImmutableMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by liaoweizhi on 2019/8/13.
 */
@RestController
@RequestMapping(value = "/v1/role/")
public class RoleController {

    @Resource
    private SysRoleService sysRoleService;

    @GetMapping("list_all")
    public List<SysRole> listAll() {
        return UserRoleScheduleManager.allRoleList;
    }

    @PostMapping("save_or_update")
    public Object add(@RequestBody SysRole request) {
        Validator.assertHasFields(StringUtils.isNotEmpty(request.getName()), "role name is required.");
        Validator.assertHasFields(StringUtils.isNotEmpty(request.getDescription()), "role description is required.");

        return ImmutableMap.of("id", sysRoleService.upsert(request));
    }

}
